package org.foxas.ui
{
	import flash.events.TimerEvent;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;

	public class FCountDown extends FLabel
	{
		public static const STOP:String = "STOP";
		private var IDtime:uint;
		public var secondMax:int = 30;
		protected var num:FLabel;
		public function FCountDown(_title:String="",_secondMax:int=30,_x:int=0, _y:int=0)
		{
			super(_title+": ",_x, _y);
			num = new FLabel("0",this.txt.width/2-10,20);
			num.size = 32;
			num.color = 0xFF3300;
			secondMax = _secondMax;
			this.addChild(num);
			this.size = 18;
			this.color = 0xFF9900;
		}
		public function show():void{
			this.visible = true;
		}
		public function play(_secondMax:int=0):void{
			this.num.visible=true;
			show();
			if(_secondMax==0){
			}else{
				this.secondMax = _secondMax;
			}
			num.text = this.secondMax.toString();
			IDtime = setInterval(run,1000);
		}
		public function run():void{
			var n:Number = Number(num.text);
			if(n==0){
				this.stop();
			}else{
				n--;
			}
			num.text = n.toString();
		}
		public function pause():void{
			clearInterval(IDtime);
		}
		public function stop():void{
			this.num.visible=false;
			clearInterval(IDtime);
			if(this.hasEventListener(FCountDown.STOP)){
				this.dispatchEvent(new TimerEvent(FCountDown.STOP));
			}
		}
		public function hidden():void{
			this.visible = false;
		}
	}
}